﻿
package {
	import com.adobe.images.*;
	import fl.controls.Button;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import fl.events.ColorPickerEvent;
	import com.quietless.bitmap.BitmapSnapshot;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.text.TextFormat;
	import flash.text.TextColorType;
	import flash.display.Sprite;
    import flash.globalization.LocaleID;
    import flash.globalization.StringTools;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
	import flash.text.Font;
	import flash.display.Bitmap;

	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix; 
	import flash.net.URLRequestHeader;
	import flash.net.URLLoaderDataFormat; 
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.SharedObject;
	import flash.utils.ByteArray
	import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
	import flash.net.navigateToURL;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.xml.*;
    import flash.events.IOErrorEvent;
	
	 public class AppMain2 extends Sprite {
		private var _draw			:Button;
		private var _server			:Button;
		private var _desktop		:Button;		
		private var _controls		:Sprite;
		//private var _canvas			:Canvas;
		
		private var _filename		:String = 'colored-balls.png';	
		private var _phpscript		:String = 'php/upload.php';
		private var _destination	:String = 'bitmap-snapshots';
		private var _param 			:String = '10';
		private var _paramFondo 	:String = '';
		private var _paramNuevoNombre :String = '0';
		private var _paramEmail 	:String = 'x';
		private var loader			:Loader;
			

		public function AppMain2()
		{
			initControls();
			
		}
		function loadingError(e:IOErrorEvent):void {
			loader.load(new URLRequest("/appCumpleanio/fla/imagenes/"+ _paramFondo +".jpg"));
		}
		function loaderComplete(myEvent:Event)
		{
		  	var myQueryStrings=this.loaderInfo.parameters;
		  	_paramFondo = myQueryStrings.FONDO.toLowerCase();
			_param = myQueryStrings.ID.toLowerCase();
			_paramNuevoNombre= myQueryStrings.ES.toLowerCase();
		  	_paramEmail = myQueryStrings.EMAIL.toLowerCase();
		  	loader = null;
			loader =new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadingError);
			
			//if(_paramNuevoNombre=='0'){
//				this.FORM1.btnEnviar.visible=false;
//			}
			//var myDate1:Date = new Date();
			//var Anio:String = myDate1.getFullYear().toString();
			var url = "/appCumpleanio/fla/imagenes/" +_paramNuevoNombre + ".jpg";
			var req:URLRequest = new URLRequest(url);

			if( versionChanged() )
			{
				req.method = URLRequestMethod.POST;
				req.data = true;
			}

			loader.load(req);
			FOTO1.addChild(loader);
		}
		function versionChanged():Boolean
		{
			var cache:SharedObject = flash.net.SharedObject.getLocal( "app-cache", "/" );
			var version:String = loaderInfo.parameters.version;
			var changed:Boolean = cache.data.version != version;
			
			cache.data.version = version;
			cache.flush();
		
			return changed;
		} 
		
			
		private function initControls():void
		{
			this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
			
			this.FORM1.cbTamanio.selectedIndex = 1;
			this.FORM1.botonAceptar.addEventListener(MouseEvent.CLICK,agregar);
			this.FORM1.btnEnviar.addEventListener(MouseEvent.CLICK,enviar);
			this.FORM1.btnForm.addEventListener(MouseEvent.CLICK,mostrarForm);
			this.FORM1.pcColor.addEventListener(ColorPickerEvent.CHANGE,changeHandler);
			this.FORM1.mensaje.addEventListener(Event.CHANGE,changeHandlerText);
			this.FORM1.cbTamanio.addEventListener(Event.CHANGE,changeHandlerComboTamanio);
			this.FORM1.cbTipo.addEventListener(Event.CHANGE,changeHandlerComboTipo);
			this.mcText.addEventListener(MouseEvent.MOUSE_OVER, hoverMouseHand);
			this.mcText.addEventListener(MouseEvent.MOUSE_DOWN, onMouse_Down);
			this.mcText.addEventListener(MouseEvent.MOUSE_UP, onMouse_Up);
		}
		
		private function onMouseClick(e:MouseEvent):void
		{
			//if (e.target==_draw) draw();
		}
		
		function hoverMouseHand(e:Event)
		{
			e.currentTarget.buttonMode = true;
			e.currentTarget.useHandCursor = true;
		}
		

		function enviar(e:Event){
			
			var header:URLRequestHeader = new URLRequestHeader ("Content-type", "text/html");
			var request:URLRequest = new URLRequest("/appCumpleanio/sendPostal.aspx?id="+_param+"&email="+_paramEmail);
			request.requestHeaders.push(header);
			request.method = URLRequestMethod.POST;
			
			var loader:URLLoader = new URLLoader();
			loader.load(request);
		}
		
		function agregar(e:Event){
			this.FORM1.y = 728;
			var stage_snapshot:BitmapData = new BitmapData(1000, 651); 
			var myRectangle:Rectangle = new Rectangle(0, 0, 1000, 651); 
			var myMatrix:Matrix = new Matrix(); 
			var translateMatrix:Matrix = new Matrix(); 
			translateMatrix.translate(0, 0); 
			myMatrix.concat(translateMatrix); 
			stage_snapshot.draw(stage,myMatrix,null,null,myRectangle); 
		
			var encoded_jpg:JPGEncoder = new JPGEncoder(100);
			var jpg_binary:ByteArray = encoded_jpg.encode(stage_snapshot);
			
			var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
			var request:URLRequest = new URLRequest("/appCumpleanio/SaveImage.aspx?id="+_param+"&es="+_paramNuevoNombre);
			request.requestHeaders.push(header);
			request.method = URLRequestMethod.POST;
			request.data = jpg_binary;
			
			var loader:URLLoader = new URLLoader();
			loader.load(request);
		}
		
		function mostrarForm(e:Event){
			if(this.FORM1.y < 700)
				while(this.FORM1.y < 728)
					this.FORM1.y = this.FORM1.y + 2;
			else
				while(this.FORM1.y > 620)
					this.FORM1.y = this.FORM1.y - 2;
		}
		
		function changeHandler(event:ColorPickerEvent):void {
			setFormato();
		}
		
		function changeHandlerText(event:Event):void {
			setFormato();
		}
		
		function changeHandlerComboTamanio(event:Event):void {
			setFormato();
		}
		
		function changeHandlerComboTipo(event:Event):void {
			setFormato();
		}
		
		function setFormato():void
		{
			this.mcText.TextoDinamico.text = this.FORM1.mensaje.text;	
			var format:TextFormat = new TextFormat();
			format.size = this.FORM1.cbTamanio.selectedItem.data;
			format.font = this.FORM1.cbTipo.selectedItem.data;
			format.color = this.FORM1.pcColor.selectedColor;
			this.mcText.TextoDinamico.setTextFormat(format);
			
		}
		
		function onMouse_Down(e:Event):void{
			this.mcText.startDrag()
		}
		function onMouse_Up(e:Event):void{
			this.mcText.stopDrag();
		}
	}
	
}
